import { create } from "zustand";
import { persist, createJSONStorage } from "zustand/middleware";
import { ITreeNode } from "../types";

type SettingStore = {
  showHidden?: boolean;
  sortType?: "modTime" | "creationTime" | "name";
  desc?: boolean;
  isLock?: boolean;
  //   unsave?: boolean;
  currentPath?: string;
  lastModTime?: number;
  update?: (s: SettingStore) => void;
};

type TreeDataStore = {
  tree?: ITreeNode[];
  updateTree?: (t: ITreeNode[]) => void;
};

const useSettingStore = create<SettingStore>()(
  persist(
    (set) => ({
      showHidden: false,
      sortType: "creationTime",
      desc: false,
      isLock: false,
      unsave: false,
      update: (store) => set((state) => ({ ...state, ...store })),
    }),
    {
      name: "gote-storage",
      storage: createJSONStorage(() => sessionStorage),
    }
  )
);

const useTreeStore = create<TreeDataStore>()((set) => ({
  tree: [],
  updateTree: (store) =>
    set((state) => ({ tree: state.tree ? [...state.tree, ...store] : store })),
}));

export { useSettingStore, useTreeStore };
